package com.fanxl.kafka;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @description:
 * @author: fanxiaole
 * @date: 2022/3/20 20:48
 */
@Component
public class KafkaSender {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendAccessLog() throws Exception {
        Access access = new Access();
        access.os = randData(new String[]{"ios", "android"});
        if ("ios".equals(access.os)) {
            access.deviceType = randData(new String[]{"iphone10", "iphone11", "iphone13", "iphone12"});
        } else {
            access.deviceType = randData(new String[]{"小米Mix3", "小米12", "华为mate3", "荣耀X30", "三星S22"});
        }
        Random random = new Random();
        int randomNumber = random.nextInt(100);
        if (randomNumber % 2 == 0) {
            access.uid = randData(new String[]{"user_1", "user_2", "user_3", "user_4", "user_5", "user_6", "user_7", "user_8", "user_9", "user_10"});
            access.device = randData(new String[]{"4fc124c6c25a4531955f035621229a38", "13cf3809eda7406e9597468d0f847621",
                    "1d8beefde9954e7c86aabff920b8ea54", "e8d7c6de319e4340b46ae7bd8b304a17", "ee0b02a2060142f9b87ab6ac4edd42ea",
                    "67779c2a828844ebb72d572b22764199", "ab54fdddbc7f4e34b8f0be6f72ea84e4", "2adc2fb052a1465e83947062f9f79f53",
                    "9a63b229f5594ceab1678c6e79191fbb", "e6636c06e10749c980901954e6e102d6"});
        } else {
            access.device = UUID.randomUUID().toString().replace("-", "");
            access.uid = "user_" + randomNumber;
        }
        access.nu = randomNumber % 2;
        access.channel = randData(new String[]{"商城", "微信", "网页"});
        access.time = System.currentTimeMillis();
//        Thread.sleep(random.nextInt(1000));
        access.event = randData(new String[]{"startup", "view", "pay", "shop-card", "exit", "error"});
        access.net = randData(new String[]{"4G", "wifi", "5G", "3G"});
        access.version = randData(new String[]{"v1.1.0", "v1.1.4", "v1.1.3", "v1.1.1"});
        access.ip = randData(new String[]{"106.80.10.98", "119.98.217.234", "180.160.110.12", "122.102.80.145", "114.247.50.2"});
        if (!"startup".equals(access.event)) {
            access.product = createProduct();
        }
        String message = JSON.toJSONString(access);
        System.out.println(message);
        kafkaTemplate.send("flink_test", message);
    }

    public String randData(String[] array) {
        Random random = new Random();
        return array[random.nextInt(array.length)];
    }

    public Product createProduct() {
        Product product = new Product();
        String[] category = new String[]{"车", "编程", "笔记本", "手机"};
        Random random = new Random();
        int randomCategory = random.nextInt(category.length);

        Map<String, List<String>> productMap = getMapProduct();
        product.category = category[randomCategory];
        List<String> productList = productMap.get(product.category);
        product.name = productList.get(random.nextInt(productList.size()));
        return product;
    }

    public Map<String, List<String>> getMapProduct() {
        Map<String, List<String>> productMap = new HashMap<>();
        productMap.put("车", Arrays.asList("宝马", "比亚迪", "奔驰", "奥迪", "吉利", "红旗", "小鹏", "蔚来"));
        productMap.put("编程", Arrays.asList("Java", "Spark", "Javascript", "CSS", "Mysql", "Flink", "Python", "GO"));
        productMap.put("笔记本", Arrays.asList("联想", "戴尔", "华硕", "小米", "华为", "宏碁"));
        productMap.put("手机", Arrays.asList("苹果", "小米", "华为", "魅族", "荣耀", "OPPO"));
        return productMap;
    }

    public static void main(String[] args) throws Exception {
        KafkaSender kafkaSender = new KafkaSender();
        for (int i = 0; i < 100; i++) {
            kafkaSender.sendAccessLog();
        }
    }




}
